import { defineStore, storeToRefs } from "pinia";
import { ref, reactive } from "vue"
import { ElMessage } from "element-plus"
import { associateMemberOrderPagePost, associateMemberCommandRefundOrderPost } from "@/api/tcl"

import { useEnumStore } from "@/store/enumeration"
import useInstance from '@/hooks/useInstance';

export const orderStatisticsPageStore = defineStore('orderStatisticsPageStoreId1', () => {
    const { global } = useInstance();

    const { orderProcessStatus, orderStatusDetails, paymentMethodList, beforeIsSign } = useEnumStore()
    const detailData = ref([
        {
            type: "text",
            prop: "supplierType",
            name: "供应商类型",
            value: null
        },
    ])
    const tableProp = reactive(
        {
            name: "orderStatisticsPage",
            propsList: [
                {
                    prop: 'supplierCode',
                    label: '日期',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'supplierCode',
                    label: '签约订单数',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'supplierCode',
                    label: '解约订单数',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'supplierCode',
                    label: '扣费订单数',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'supplierCode',
                    label: '退款订单数',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'supplierCode',
                    label: '扣费金额',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'supplierCode',
                    label: '退款金额',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'supplierCode',
                    label: '创建时间',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },

            ]
        })
    const listSearch = reactive<any>({
        orders: ["created_time desc"],
        condition: {
            supplierCode: null,//供应商编码 "TCL" 
 
        },
        pageSize: 20,
        pageNum: 1
    });
    const pageParams = reactive({
        tableHeight: 400,
        total: 0,
        loadIng: false,
        tableData: [],
    })
    const operateTime = ref<any>([])

    const getData1 = async (type: string) => {

        pageParams.loadIng = true
        if (type == 'reset') {
            listSearch.pageNum = 1
            listSearch.pageSize = 20
            Object.keys(listSearch.condition).forEach((it: any) => {
                const arr = ['supplierCode','orderStatus','orderTimeStart','orderTimeEnd']
                if (!arr.includes(it)) {
                    listSearch.condition[it] = null
                }

            })
        }
        try {
            const res = await associateMemberOrderPagePost(listSearch)
            if (res.code == 20000) {
                pageParams.tableData = res.data.records.map((item: any) => {

                    return {
                        ...item,
                   
                    }
                })
                pageParams.total = res.data.total
                pageParams.loadIng = false
            }
        } catch (error) {
            pageParams.loadIng = false
            pageParams.tableData = []
            pageParams.total = 0
        }

    }
    const handPage = (page: any) => {
        console.log(page);
        listSearch.pageNum = page.pageNum
        listSearch.pageSize = page.pageSize
        getData1('search')
    }


    const timeChange = (time: any) => {
        console.log(time);
        if (time) {
            listSearch.condition.orderTimeStart = time[0]
            listSearch.condition.orderTimeEnd = time[1]
        } else {
            listSearch.condition.orderTimeStart = null
            listSearch.condition.orderTimeEnd = null
            operateTime.value = []
        }
        getData1('search')
    }

    return {
        operateTime, detailData, tableProp, listSearch, pageParams,
        getData1, timeChange, handPage
    }
})

